<template>
  <!-- 门诊医生-中医处方 -->
  <a-spin :spinning="loading">
    <div>
      <TCMList
        v-for="(orderItem, index) in orderDataSource"
        :key="orderItem.cfbh"
        :order-index="index"
        :disabled="disabled"
        :order-item="orderItem"
        :jzbh="jzbh"
        :one-order-address="orderDataSource[0]?.express"
        @delete-order="onDeleteOrder"
        @refresh="getData"
      />
      <div class="text-right pr-8 font-bold text-base">合计：{{ formatAmount(orderTotal) }}元</div>
      <a-space class="flex justify-end mt-3">
        <a-button :disabled="disabled" type="primary" ghost @click="onAddOrder">新增处方</a-button>
        <a-button :disabled="disabled" type="primary" @click="onSign">签署</a-button>
      </a-space>
    </div>
    <a-empty v-if="orderDataSource.length === 0" class="mt-20" />
  </a-spin>
</template>

<script setup>
import { ref, watchEffect, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import useStore from '../../store'
import TCMList from './TCMList'

const props = defineProps({
  jzbh: {
    type: String,
    default: ''
  },
  disabled: {
    type: Boolean,
    default: false
  }
})
const store = useStore()
const orderDataSource = ref([])
const loading = ref(false)

const onAddOrder = async () => {
  const params = {
    keycode: props.jzbh,
    zylb: '饮片' //药品类别 饮片/颗粒
    // isUseOneAddress: orderDataSource.value.length > 0 ? 1 : 0, //是否使用第一个地址
    // kdaddr: orderDataSource.value[0]?.express?.addressDetail,
    // patientAddressId: orderDataSource.value[0]?.express?.id
    // cfbh: orderDataSource.value.length + 1 //处方序号
  }
  const { code, msg } = await post('/outp/outpClinicalOrder/doSaveOutpTCMHead', params)
  if (code === 0) {
    getData()
  } else {
    message.error(msg)
  }
}
const onDeleteOrder = async (cfdh) => {
  const { code, msg } = await post('/outp/outpClinicalOrder/doDeleteOutpTCMHead', { cfdh })
  if (code !== 0) return message.error(msg || '删除失败')
  message.success('删除成功')
  getData(true)
}

// 签署
const onSign = async () => {
  const medicineData = []
  for (const item of orderDataSource.value) {
    const { code, data, msg } = await post('/outp/outpClinicalOrder/getOutpTCMDetailList', { cfdh: item.cfdh })
    if (code !== 0) {
      message.error(msg)
      continue
    }
    medicineData.push(...data)
    item.express.yzid && medicineData.push({ yzid: item.express.yzid })
    item.processMethod.yzid && medicineData.push({ yzid: item.processMethod.yzid })
    item.djYzid && medicineData.push({ yzid: item.djYzid })
  }
  const ids = medicineData.map((item) => item.yzid).filter((item) => item)
  if (ids.length === 0) return message.error('不能为空')
  const { code, msg } = await post('/outp/mzys/signedDoctor', { yzid: ids, yzzt: 1 })
  if (code !== 0) return message.error(msg || '签署失败')
  message.success('签署成功')
  getData()
}

//获取数据
const getData = async (isAdd = false) => {
  loading.value = true
  const { code, data = [], msg } = await post('/outp/outpClinicalOrder/getOutpTCMHeadList', { jzbh: [props.jzbh] })
  loading.value = false
  if (code !== 0) return message.error(msg)
  if (data.length === 0) {
    orderDataSource.value = []
    //没有数据则新增一条
    isAdd && onAddOrder()
  } else {
    orderDataSource.value = data || []
    getTotalCost()
  }
}
const orderTotal = ref(0)
const getTotalCost = async () => {
  const res = await post('outp/outpClinicalOrder/getOutpTCMTotalCost', { jzbh: [store.currentPatient.jzbh] })
  if (res.code !== 0) return message.error(res.msg || '获取费用失败')
  orderTotal.value = res.data
}
watch(
  () => store.tempTCM,
  (val) => {
    if (val) {
      val.isNew = true
      orderDataSource.value.push(val)
    }
  }
)

watchEffect(() => {
  props.jzbh && getData(true)
})
</script>
<style lang="less" scoped>
.order-header {
  background-color: var(--thin-color);
  border-radius: 2px;
  &:first-child {
    margin-top: 0;
    border: none;
  }
}

:deep(.border-b-style) {
  border-bottom: 1px solid transparent;
  border-radius: 0;
  cursor: pointer;
  &:hover {
    border-bottom: 1px solid var(--primary-color);
  }
}
.input-style {
  padding: 0 12px;
  :deep(.ant-input) {
    padding-right: 4px;
    text-align: center !important;
    border-bottom: 1px solid var(--border-color) !important;
    cursor: pointer;
    &:hover {
      border-bottom-color: var(--primary-color) !important;
    }
  }
}
:deep(.ant-input-clear-icon) {
  display: none;
}
</style>
